home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / ici / ici.cpi / trace.c < prev    next >
C/C++ Source or Header  |  1994-10-27  |  4KB  |  243 lines

  1. /*
  2.  * trace.c - tracing for ICI
  3.  *
  4.  *
  5.  * Andy Newman (andy@research.canon.oz.au)
  6.  *
  7.  *
  8.  * This is public domain code. Use how you wish.
  9.  *
  10.  */
  11.  
  12. #include "func.h"
  13. #include "object.h"
  14. #include "trace.h"
  15. #include "file.h"
  16. #include "set.h"
  17. #include "struct.h"
  18. #include "array.h"
  19. #include "re.h"
  20. #include "str.h"
  21. #include "int.h"
  22. #include "float.h"
  23. #include "exec.h"
  24. #include "op.h"
  25.  
  26. int     trace_flags = TRACE_ALL; /* What we are tracing */
  27. int     trace_yes = 0;          /* Non-zero if tracing enabled */
  28.  
  29. static int
  30. f_trace()
  31. {
  32.     register char **s;
  33.     register int flags = 0;
  34.     register int reset = 0;
  35.     char **wds;
  36.     char *str;
  37.     if (NARGS() == 0)
  38.         return int_ret((long)trace_yes);
  39.     if (typecheck("s", &str))
  40.         return argerror(0);
  41.     wds = smash(str, ' ');
  42.     for (s = wds; *s != 0; ++s)
  43.     {
  44.     if (!strcmp(*s,"lexer"))
  45.     {
  46.         flags |= TRACE_LEXER;
  47.         reset = 1;
  48.     }
  49.     else if (!strcmp(*s,"expr"))
  50.     {
  51.         flags |= TRACE_EXPR;
  52.         reset = 1;
  53.     }
  54.     else if (!strcmp(*s,"calls"))
  55.     {
  56.         flags |= TRACE_INTRINSICS | TRACE_FUNCS;
  57.         reset = 1;
  58.     }
  59.         else if (!strcmp(*s,"funcs"))
  60.         {
  61.         flags |= TRACE_FUNCS;
  62.         reset = 1;
  63.         }
  64.     else if (!strcmp(*s,"all"))
  65.     {
  66.         flags |= TRACE_ALL;
  67.         reset = 1;
  68.     }
  69.     else if (!strcmp(*s,"mem"))
  70.     {
  71.         flags |= TRACE_MEM;
  72.         reset = 1;
  73.     }
  74.     else if (!strcmp(*s,"src"))
  75.     {
  76.         flags |= TRACE_SRC;
  77.         reset = 1;
  78.     }
  79.     else if (!strcmp(*s,"gc"))
  80.     {
  81.         flags |= TRACE_GC;
  82.         reset = 1;
  83.     }
  84.     else if (!strcmp(*s,"none"))
  85.     {
  86.         flags = 0;
  87.         reset = 1;
  88.     }
  89.     else if (!strcmp(*s,"off"))
  90.         trace_yes = 0;
  91.     else if (!strcmp(*s,"on"))
  92.         trace_yes = 1;
  93.     else
  94.     {
  95.         error = "unrecognised trace option";
  96.         zfree((char *)wds);
  97.         return 1;
  98.     }
  99.     }
  100.     if (reset)
  101.     trace_flags = flags;
  102.     zfree((char *)wds);
  103.     return int_ret(trace_yes);
  104. }
  105.  
  106. cfunc_t    trace_cfuncs[] =
  107. {
  108.     {CF_OBJ,    "trace",    f_trace},
  109.     {CF_OBJ}
  110. };
  111.  
  112. static char *
  113. fixup(s)
  114. char *s;
  115. {
  116.     static char buffer[128]; /* kludge */
  117.     register char *p;
  118.  
  119.     for (p = buffer; *s && ((p - buffer) < 125); ++s)
  120.     {
  121.     switch (*s)
  122.     {
  123.     case '\b':
  124.         *p++ = '\\';
  125.         *p++ = 'b';
  126.         break;
  127.     case '\f':
  128.         *p++ = '\\';
  129.         *p++ = 'f';
  130.         break;
  131.     case '\n':
  132.         *p++ = '\\';
  133.         *p++ = 'n';
  134.         break;
  135.     case '\r':
  136.         *p++ = '\\';
  137.         *p++ = 'r';
  138.         break;
  139.     case '\t':
  140.         *p++ = '\\';
  141.         *p++ = 't';
  142.         break;
  143.     case '\033':
  144.         *p++ = '\\';
  145.         *p++ = 'e';
  146.         break;
  147.     default:
  148.         if (*s >= ' ' && *s <= '~')
  149.         *p++ = *s;
  150.         else
  151.         {
  152.         char num[8], *pp;
  153.         *p++ = '\\';
  154.         if ( *s < ' ')
  155.             sprintf(num, "%03o", *s);
  156.         else
  157.             sprintf(num, "x%0x", *s);
  158.         for (pp = num; *pp; *p++ = *pp++)
  159.             ;
  160.         }
  161.         break;
  162.     }
  163.     }
  164.     *p = 0;
  165.     return buffer;
  166. }
  167.  
  168. static void
  169. pcall_arg(ap)
  170. object_t *ap;
  171. {
  172.     if (isint(ap))
  173.     fprintf(stderr, "%ld", intof(ap)->i_value);
  174.     else if (isstring(ap))
  175.     fprintf(stderr, "\"%s\"", fixup(stringof(ap)->s_chars));
  176.     else if (isfloat(ap))
  177.     fprintf(stderr, "%.9g", floatof(ap)->f_value);
  178.     else if (isarray(ap))
  179.     fprintf(stderr, "[array]");
  180.     else if (isset(ap))
  181.     fprintf(stderr, "[set]");
  182.     else if (isstruct(ap))
  183.     fprintf(stderr, "[struct]");
  184.     else if (isfile(ap))
  185.     fprintf(stderr, "<file>");
  186.     else if (isregexp(ap))
  187.     fprintf(stderr, "#regexp#");
  188.     else if (isfunc(ap))
  189.     fprintf(stderr, "[func]");
  190. }
  191.  
  192. void
  193. trace_pcall(o)
  194. object_t *o;
  195. {
  196.     func_t   *f = NULL;
  197.     char     *s;
  198.     int         n;
  199.     object_t **ap;
  200.     if (o->o_flags & O_CFUNC)
  201.     {
  202.         if (!(trace_flags & TRACE_INTRINSICS))
  203.             return;
  204.         s = ((cfunc_t *)o)->cf_name;
  205.     }
  206.     else if (!(trace_flags & TRACE_FUNCS))
  207.         return;
  208.     else
  209.     {
  210.         f = (func_t *)o;
  211.         s = f->f_name->s_chars;
  212.     }
  213.     fprintf(stderr, "trace: %s(", s);
  214.     n = NARGS();
  215.     ap = ARGS();
  216.     if (ap != 0)
  217.     {
  218.     if (f != NULL)
  219.     {
  220.         object_t **fp;
  221.         for
  222.         (
  223.         fp = f->f_args->a_base;
  224.         fp < f->f_args->a_top && n > 0;
  225.         ++fp, --ap
  226.         )
  227.         {
  228.         fprintf(stderr, "%s = ", stringof(*fp)->s_chars);
  229.         pcall_arg(*ap);
  230.         if ( --n > 0 )
  231.             fprintf(stderr, ", ");
  232.         }
  233.     }
  234.     while ( n > 0 )
  235.     {
  236.         pcall_arg(*ap--);
  237.         if ( --n > 0 )
  238.         fprintf(stderr, ", ");
  239.     }
  240.     }
  241.     fprintf(stderr, ")\n" );
  242. }
  243.